<template>
  <div class='container'>
    <!-- 卡片视图区 -->
    <el-card class="box-card">
      <!-- 头部搜索区 -->
      <el-form :inline="true" :model="queryInfo" ref="queryInfo" class="demo-form-inline">
        <el-form-item label="目录名称" popr='directoryName'>
          <el-input v-model="queryInfo.directoryName" @keyup.enter="handleFilter"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select v-model="queryInfo.state" @keyup.enter="handleFilter" placeholder="请选择">
            <el-option label="启用" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="onSubmit">清除</el-button>
        </el-form-item>
        <el-form-item class="XinZeng">
          <el-button type="primary" @click="handleFilter">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" class="el-icon-edit" @click="xinZeng"> 新增目录</el-button>
        </el-form-item>
      </el-form>
      <!-- /头部搜索区 -->

      <!-- 数据统计 -->
      <div class="su-ji-tong-ji">
        <i class="el-icon-info"></i>
        <span class="el-alert__title">{{alertText}}</span>
      </div>
      <!-- /数据统计 -->

      <!-- 表格 -->
    <el-table
      :data="directoryList"
      v-loading="listLoading"
      >
      <el-table-column
        type='index'
        label="序号"
        >
      </el-table-column>
      <el-table-column
        prop="subjectName"
        label="所属科学"
        >
      </el-table-column>
      <el-table-column
        prop="directoryName"
        label="目录名称">
      </el-table-column>
      <el-table-column
        prop="username"
        label="创建者">
      </el-table-column>
      <el-table-column
        prop="addDate"
        label="创建日期">
      </el-table-column>
      <el-table-column
        prop="totals"
        label="面试题数量">
      </el-table-column>
      <el-table-column
        label="状态">
        <template slot-scope="scope">
         {{scope.row.state == 1 ? '已启用' : '已禁用'}}
        </template>
      </el-table-column>
      <el-table-column
      fixed="right"
      label="操作"
      width="120">
      <template slot-scope="scope" >
        <el-button
          @click.native.prevent="deleteRow(scope.$index, tableData)"
          type="text"
          @click="scope.row.state == '1' ? `${scope.row.state='0'}` : `${scope.row.state='1'}`"
          size="small">
          {{scope.row.state == '1' ? '禁用' : '启用'}}
        </el-button>
        <el-button
          @click.native.prevent="deleteRow(scope.$index, tableData)"
          type="text"
          size="small">
          修改
        </el-button>
        <el-button
          @click.native.prevent="deleteRow(scope.$index, tableData)"
          type="text"
          @click="shanChu"
          size="small">
          删除
        </el-button>
      </template>
    </el-table-column>
    </el-table>
    <!-- /表格 -->

      <!-- 分页 -->
      <div class="fen-ye">
          <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="Number(queryInfo.page)"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="Number(queryInfo.pagesize)"
          layout="prev, pager, next,sizes, jumper"
          :total="Number(total)"
          background>
          </el-pagination>
        </div>
      <!-- 分页 -->
      <!-- 新增标签弹层 -->
      <Dialog :dialogVisible.sync="dialogVisible" @close="guanBiXingZeng"></Dialog>
    </el-card>
    <!-- /卡片视图区 -->
  </div>
</template>

<script>
import { list } from '@/api/hmmm/directorys.js'
import Dialog from './../components/directorys-add.vue'
export default {
  components: {
    Dialog
  },
  data () {
    return {
      dialogVisible: false,
      formInline: {
        user: '',
        region: ''
      },
      listLoading: true,
      // 统计
      alertText: '',
      directoryList: [], // 目录列表
      total: null,
      // 获取用户列表的参数对象
      queryInfo: {
        // 当前页数
        page: 1,
        // 当前每页显示多少条数据
        pagesize: 10
      }
      // formData: {
      //   isFamous: '',
      //   shortName: '',
      //   company: '',
      //   province: '',
      //   city: '',
      //   tags: '',
      //   remarks: ''
      // },
    }
  },
  created () {
    this.loadList()
  },
  methods: {
    // 初始数据
    initialDate () {
      // 读取数据
      this.loadList()
    },
    onSubmit () {
      console.log('submit!')
    },
    async loadList (params) {
      try {
        this.listLoading = true
        const { data: res } = await list(this.queryInfo)
        this.directoryList = res.items
        // 获取总条数
        this.total = res.counts
        this.alertText = `数据共 ${this.total} 条`
        console.log(res)
        console.log(this.directoryList)
        this.listLoading = false
      } catch (err) {
        this.$toast('获取目录列表失败')
      }
    },
    // 重置
    resetForm () {
      this.$refs.queryInfo.resetFields()
    },
    // 搜索信息
    handleFilter () {
      this.queryInfo.page = 1
      this.loadList(this.queryInfo)
    },
    // 清除
    // onSubmit () {
    //   this.queryInfo.page = 1
    //   this.queryInfo.directoryName = ''
    //   this.queryInfo.state = null
    //   this.loadList(this.queryInfo)
    // },
    // 监听pagesize 改变的事件
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      if (this.queryInfo.page === 1) {
        this.loadList(this.queryInfo)
      }
    },
    // 监听页码值改变事件
    handleCurrentChange (newPage) {
      this.queryInfo.page = newPage
      this.loadList()
    },
    // 新增弹框
    xinZeng () {
      this.dialogVisible = true
    },
    // 关闭弹框
    guanBiXingZeng () {
      this.dialogVisible = false
    },
    // 删除弹框
    shanChu () {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style scoped lang='less'>
.container {
  .el-card {
    margin: 10px 10px;
    .el-form {
      display: flex;
      .XinZeng {
         flex: 1
      }
      /deep/.el-form-item__label {
      width: 80px;
      line-height: 32px;
      }
    }
    .su-ji-tong-ji {
      background: #f4f4f5;
      color: #909399;
      border-radius: 4px;
      height: 36px;
      padding: 8px 16px;
      margin-bottom: 15px;
      .el-alert__title {
        font-size: 13px;
        line-height: 18px;
        margin: 0;
        padding: 0 8px;
      }
    }
    /deep/.el-table th.el-table__cell {
    background-color: #fafafa;
    border-bottom: 2px solid #e8e8e8;
    }
    .fen-ye {
    margin-top: 20px;
    padding: 2px 5px;
    display: flex;
    justify-content: flex-end;
    }

  }

}
</style>
